{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 作业\n",
    "\n",
    "### 1.查询所有上市公司的五张财务表，2015-2020q2期间每个季度的所有财务数据，存储进本地数据库中。\n",
    "### 2.计算从下载到存储成功过程的耗时。\n",
    "### 3.五张财务表：'市值表','资产负债表','现金流数据表','利润数据表','财务指标数据表'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 注意：由于最后生成的表格内存过大，无法上传到 码云，故缩短时间间期为 “2018-2020q2”，同时只上传“市值表”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成 时间列表\n",
    "date_list = []\n",
    "# for year in range(2015,2021):\n",
    "for year in range(2018,2021):\n",
    "    # 生成 年\n",
    "    for quarter in range(1,5):\n",
    "        #生成 季度\n",
    "        if year == 2020 and quarter > 2:\n",
    "            # 要求数据截止到 2020q2 之前\n",
    "            break\n",
    "        else:\n",
    "            # 设置格式： '2015q1'\n",
    "            date = str(year) + 'q' + str(quarter)\n",
    "            # 添加到时间列表中\n",
    "            date_list.append(date)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['2018q1',\n",
       " '2018q2',\n",
       " '2018q3',\n",
       " '2018q4',\n",
       " '2019q1',\n",
       " '2019q2',\n",
       " '2019q3',\n",
       " '2019q4',\n",
       " '2020q1',\n",
       " '2020q2']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "def save_excel(name, type):\n",
    "    # 创建文件保存函数，其中  name：文件名； type：表格类型（五种表）\n",
    "    \n",
    "    # 创建excel表格，写入\n",
    "    writer = pd.ExcelWriter(name)\n",
    "    # 循环季度列表，以 年-季度 命名 sheet表\n",
    "    for date in date_list:\n",
    "        # 获取不同种类表格的具体信息\n",
    "        df = get_fundamentals(type, statDate = date)\n",
    "        # 将信息写入excel表中\n",
    "        # to_excel(文件名，sheet名)\n",
    "        df.to_excel(writer,str(date))\n",
    "    # 保存表格\n",
    "    writer.save()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "code_folding": []
   },
   "outputs": [],
   "source": [
    "\n",
    "def start(tables,excel_names):\n",
    "    # 创建函数，依次保存 五个表 的数据\n",
    "    for index,table in enumerate(tables):\n",
    "        # 查找 表格 信息\n",
    "        # eval(str)：去除字符串两端的引号\n",
    "        q = query(eval(table + '.code'), eval(table))\n",
    "        # 调用 save_excel（） 函数保存数据\n",
    "        # %time ： 计算数据下载时间\n",
    "        %time save_excel(excel_names[index] + '.xls', q)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 10.3 s, sys: 156 ms, total: 10.4 s\n",
      "Wall time: 13.3 s\n",
      "CPU times: user 52.5 s, sys: 668 ms, total: 53.2 s\n",
      "Wall time: 1min 7s\n",
      "CPU times: user 38.2 s, sys: 332 ms, total: 38.5 s\n",
      "Wall time: 49.7 s\n",
      "CPU times: user 28.8 s, sys: 316 ms, total: 29.1 s\n",
      "Wall time: 37.7 s\n",
      "CPU times: user 25.3 s, sys: 288 ms, total: 25.6 s\n",
      "Wall time: 32.9 s\n"
     ]
    }
   ],
   "source": [
    "# 表格类型\n",
    "tables = ['valuation', 'balance', 'cash_flow', 'income', 'indicator']\n",
    "# 对应保存的文件名\n",
    "excel_names = ['市值表','资产负债表','现金流数据表','利润数据表','财务指标数据表']\n",
    "# 调用函数\n",
    "start(tables, excel_names)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "MarkDown菜单",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
